package com.shxc.smarttrade.old.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.HomePagerDatas;
import com.shxc.smarttrade.old.myinterface.MasterItemClickListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jcdev1 on 2017/5/16.
 */

public class MasterListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<HomePagerDatas.DataBean.MastersBean> mastersBeen;
    private LayoutInflater mInflater;
    private MasterItemClickListener listener;

    public MasterListAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);

    }

    public void setDatas(List<HomePagerDatas.DataBean.MastersBean> mastersBeen) {
        this.mastersBeen = mastersBeen;
        notifyDataSetChanged();
    }

    public void setMyItemCLickListener(MasterItemClickListener listener) {
        this.listener = listener;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == 0)
            return new MasterHolder(mInflater.inflate(R.layout.adapter_foot_one, parent, false));
        else
            return new MoreHolder(mInflater.inflate(R.layout.adapter_click_load_more, parent, false));

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MasterHolder) {
            final HomePagerDatas.DataBean.MastersBean mb = mastersBeen.get(position);
            Glide.with(mContext).load(mb.getHeadUrl()).into(((MasterHolder) holder).ivHead);
            ((MasterHolder) holder).tvName.setText(mb.getUsername());
            ((MasterHolder) holder).tvToal.setText(mb.getTotalProfit() + "%");
            ((MasterHolder) holder).tvHistory.setText(mb.getHistorySum() + "");
            ((MasterHolder) holder).tvPrecisionRate.setText(mb.getSuccRate() + "%");
            ((MasterHolder) holder).tvTotalVolume.setText(mb.getLot() + "");
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.ItemViewClickListener(mb, position);
                }
            });
            ((MasterHolder) holder).btnBuy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.ButtonClickListener(mb, position);
                }
            });
        } else {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onMoreClick(position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mastersBeen == null ? 0 : mastersBeen.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mastersBeen.size())
            return 1;
        else
            return 0;
    }

    public class MasterHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_head)
        ImageView ivHead;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_totalprofit)
        TextView tvToal;
        @BindView(R.id.tv_historysum)
        TextView tvHistory;
        @BindView(R.id.btn_buy)
        Button btnBuy;
        @BindView(R.id.tv_total_volume)
        TextView tvTotalVolume;
        @BindView(R.id.tv_precision_rate)
        TextView tvPrecisionRate;

        MasterHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            AutoUtils.autoSize(view);
        }
    }

    public class MoreHolder extends RecyclerView.ViewHolder {
        MoreHolder(View view) {
            super(view);
        }
    }
}
